package autoupdate.test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.ConnectException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import static autoupdate.test.AutoUpdateConstants.*;

import common.test.Module;
import common.test.VersionNumber;

public class VersionFileReaderV2{

	public static List<Module> getModuleUpdateList(){

		Map<String, Module> currentModules;
		Map<String, Module> newestModules = new HashMap<String, Module>();
		List<Module> staleModules = new ArrayList<Module>();

		currentModules = readModuleVersionFile(CURRENT_VERSIONS_FILE, false);

		List<String> updateSites = getUpdateSites();
		int siteCount = updateSites.size();
		
		for(int i = 0; i < siteCount; i++){
			newestModules.putAll(readModuleVersionFile(updateSites.get(i), true));
		}
		
		Iterator<String> i2 = newestModules.keySet().iterator();
		while(i2.hasNext()){
			String moduleName = i2.next();
			/*
			 * System.out.println(moduleName);
			 * System.out.println(currentModules.get(moduleName));
			 * System.out.println(newestModules.containsKey(moduleName));
			 */
			if(currentModules.containsKey(moduleName)){
				// if < 0, need update
				if(currentModules.get(moduleName).compareTo(newestModules.get(moduleName)) < 0){
					staleModules.add(newestModules.get(moduleName));
					// currentVersions.setProperty(arg0, arg1)
				}
			}else{
				staleModules.add(newestModules.get(moduleName));
			}
		}

		return staleModules;
	}

	private static Map<String, Module> readModuleVersionFile(String propertyFile, boolean isURL){
		Map<String, Module> moduleMap = new HashMap<String, Module>();

		try{
			InputStream stream = null;
			if(isURL){
				URL url = new URL(propertyFile);
				stream = url.openStream();
			}else
				stream = new FileInputStream(propertyFile);
			Properties properties = new Properties();
			properties.load(stream);
			stream.close();

			//System.out.println("***** " + propertyFile + " *****");
			Collection<Object> collection = properties.values();
			Iterator<Object> i = collection.iterator();
			String version;
			String[] splitString;
			while(i.hasNext()){
				version = (String) i.next();
				splitString = version.split("[|]");
				String moduleFilename = splitString[1].trim();
				//System.out.println(moduleFilename + " " + splitString[0].trim());
				String moduleName = moduleFilename.substring(0, moduleFilename.lastIndexOf("."));
				if(splitString.length == 2)
					moduleMap.put(moduleFilename, new Module(moduleName, new VersionNumber(splitString[0].trim()), moduleFilename));
				else if(splitString.length == 3)
					moduleMap.put(moduleFilename, new Module(moduleName, new VersionNumber(splitString[0].trim()), moduleFilename, splitString[2].trim()));
				else if(splitString.length == 4){
					Module module = new Module(moduleName, new VersionNumber(splitString[0].trim()), moduleFilename, splitString[2].trim());
					String[] splitStrCS = splitString[3].trim().split(":");
					module.setChecksumType(splitStrCS[0].trim());
					module.setChecksumValue(splitStrCS[1].trim());
					moduleMap.put(moduleFilename, module);
				}
				else
					throw new Exception("invalid version split: " + splitString.length);
			}
		}catch(ConnectException e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch(IOException e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch(Exception e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return moduleMap;
	}

	public static Properties getCurrentVersionProperties(){

		Properties properties = new Properties();
		try{
			InputStream stream = null;

			stream = new FileInputStream(CURRENT_VERSIONS_FILE);
			properties.load(stream);
			stream.close();

		}catch(IOException e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch(Exception e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return properties;
	}
	
	public static List<String> getUpdateSites(){
		List<String> siteList = new ArrayList<String>();
		Properties updateSites = new Properties();
		FileInputStream stream;
		try{
			stream = new FileInputStream(UPDATE_SITE_FILE);
			updateSites.load(stream);
			stream.close();
			
			Collection<Object> collection = updateSites.values();
			Iterator<Object> i = collection.iterator();
			Object o;
			while(i.hasNext()){
				o = i.next();
				siteList.add((String)o);
			}
		}catch(FileNotFoundException e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch(IOException e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return siteList;
	}
}
